package com.xunfang.dead;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

/**
 * @author yellow Doctor
 * @date 2025-11-12
 * @desc https://blog.csdn.net/hekai7217
 *
 * 01  普通
 *        交换机、 normal.exchange
 *        路由、   dg.gk
 *        队列     normal.queue
 *
 * 02  死信
 *          交换机、dlx.exchange
 *          路由、  dlx.queue
 *          队列    dead.key
 *
 * 03 普通队列绑定 死信交换机 路由
 */
@Configuration
public class MQConfig {

    //普通
    @Bean
    public Queue queue() {
        return QueueBuilder.durable("normal.queue")  //普通队列名字
                .deadLetterExchange("dlx.exchange")    //死信交换机
                .deadLetterRoutingKey("dead.key")  //死信路由键
                .maxLength(2)
                .ttl(20000)   //设置消息过期时间为20秒，消息20秒还没有被消费就进入到死信队列
                .build();
    }


    @Bean
    public DirectExchange directExchange() {
        return new DirectExchange("normal.exchange");
    }

    @Bean
    public Binding binding(Queue queue,DirectExchange directExchange) {
        return BindingBuilder.bind(queue).to(directExchange).with("dg.gk");
    }


    //死信
    @Bean
    public Queue deadQueue() {
        return new Queue("dlx.queue");
    }

    @Bean
    public DirectExchange DeadExchange() {
        return new DirectExchange("dlx.exchange");
    }

    @Bean
    public Binding DeadBinding(Queue deadQueue,DirectExchange DeadExchange) {
        return BindingBuilder.bind(deadQueue).to(DeadExchange).with("dead.key");
    }

}
